%% @doc This module is used for parsing messages that shall be sent to the game server.
%% @end

-module(msg).
-export([parse_message/6,init/6]).

%% @doc Spawns the parse_message process
%% @spec init(Game,Name,Pos,Host,Pw,IPid) -> Pid
%% @end
init(Game,Name,Pos,Host,Pw,IPid) -> spawn(fun() -> parse_message(Game,Name,Pos,Host,Pw,IPid) end).

%% @doc Recieves messages, parses them and sends them to the interface.
%% @spec parse_message(Game, Name, Pos, Host, Pw, IPid) -> ok
%% @end
parse_message(Game, Name, Pos, Host, Pw, IPid) ->
	receive
		{putpiece,Type,Coord} -> IPid ! {smessage,list_to_binary(integer_to_list(1009)++"|"++Game++","++Pos++","++integer_to_list(Type)++","++integer_to_list(Coord))};
		{gametextmsg,Msg} -> IPid ! {smessage,list_to_binary(integer_to_list(1010)++"|"++Game++","++Name++","++Msg)};
		{leavegame} -> IPid ! {smessage,list_to_binary(integer_to_list(1011)++"|"++Name++","++Host++","++Game)};
		{sitdown} -> IPid ! {smessage,list_to_binary(integer_to_list(1012)++"|"++Game++","++Name++","++Pos++","++"false")};
		{joingame} -> IPid ! {smessage,list_to_binary(integer_to_list(1013)++"|"++Name++","++Pw++","++Host++","++Game)};
		{deletegame} -> IPid ! {smessage,list_to_binary(integer_to_list(1015)++"|"++Game)};
		{startgame} -> IPid ! {smessage,list_to_binary(integer_to_list(1018)++"|"++Game)};
		{rolldice} -> IPid ! {smessage,list_to_binary(integer_to_list(1031)++"|"++Game)};
		{endturn} -> IPid ! {smessage,list_to_binary(integer_to_list(1032)++"|"++Game)};
		{discard,[G1,G2,G3,G4,G5]} -> IPid ! {smessage,list_to_binary(integer_to_list(1033)++"|"++Game++","++integer_to_list(G1)++","++integer_to_list(G2)++","++integer_to_list(G3)++","++integer_to_list(G4)++","++integer_to_list(G5)++","++integer_to_list(0))},
							io:fwrite("Skickat till interface!~n");
		{moverobber,Coord} -> IPid ! {smessage,list_to_binary(integer_to_list(1034)++"|"++Game++","++Pos++","++integer_to_list(Coord))};
		{chooseplayer,Player} -> IPid ! {smessage,list_to_binary(integer_to_list(1035)++"|"++Game++","++integer_to_list(Player))};
		{rejectoffer} -> IPid ! {smessage,list_to_binary(integer_to_list(1037)++"|"++Game++","++Pos)};
		{clearoffer} -> IPid ! {smessage,list_to_binary(integer_to_list(1038)++"|"++Game++","++Pos)};
		{acceptoffer,OfferingPlayer} -> IPid ! {smessage,list_to_binary(integer_to_list(1039)++"|"++Game++","++Pos++","++integer_to_list(OfferingPlayer))};
		{banktrade,[Gi1,Gi2,Gi3,Gi4,Gi5],[Ge1,Ge2,Ge3,Ge4,Ge5]} -> IPid ! {smessage,list_to_binary(integer_to_list(1040)++"|"++Game++","++integer_to_list(Gi1)++","++integer_to_list(Gi2)++","++integer_to_list(Gi3)++","++integer_to_list(Gi4)++","++integer_to_list(Gi5)++","++integer_to_list(Ge1)++","++integer_to_list(Ge2)++","++integer_to_list(Ge3)++","++integer_to_list(Ge4)++","++integer_to_list(Ge5))};
		{makeoffer,To,Give,Get} -> IPid ! {smessage,list_to_binary(integer_to_list(1041)++"|"++Game++","++Pos++","++To++","++lists:map(fun erlang:integer_to_list/1,Give)++","++lists:map(fun erlang:integer_to_list/1,Get))}; %To är en lista av bool där erbjudandet ges till spelaren som sitter på en position, pos, där To[Pos] = true 
		{buildrequest,Type} -> IPid ! {smessage,list_to_binary(integer_to_list(1043)++"|"++Game++","++integer_to_list(Type))};
		{cancelbuildrequest,Type} -> IPid ! {smessage,list_to_binary(integer_to_list(1044)++"|"++Game++","++Type)};
		{buycardrequest} -> IPid ! {smessage,list_to_binary(integer_to_list(1045)++"|"++Game)};
		{playdevcardrequest,CardType} -> IPid ! {smessage,list_to_binary(integer_to_list(1049)++"|"++Game++","++integer_to_list(CardType))}; %See DevCardConstants for CardTypes
		{discoverypick,[G1,G2,G3,G4,G5]} -> IPid ! {smessage,list_to_binary(integer_to_list(1052)++"|"++Game++","++integer_to_list(G1)++","++integer_to_list(G2)++","++integer_to_list(G3)++","++integer_to_list(G4)++","++integer_to_list(G5)++","++integer_to_list(0))};
		{monopolypick,Res} -> IPid ! {smessage,list_to_binary(integer_to_list(1053)++"|"++Game++","++Res)};
		{changeface,Face} -> IPid ! {smessage,list_to_binary(integer_to_list(1058)++"|"++Game++","++Pos++","++Face)};
		{resetboardrequest} -> IPid ! {smessage,list_to_binary(integer_to_list(1073)++"|"++Game)};
		{resetboarvote,Vote} -> IPid ! {smessage,list_to_binary(integer_to_list(1076)++"|"++Game++","++Pos++","++Vote)};
		{newgamewithoptionsrequest} -> IPid ! {smessage,list_to_binary(integer_to_list(1078)++"|"++Name++","++Pw++","++Host++","++Game++",-")};
		{gameoptiongetdefaults} -> IPid ! {smessage,list_to_binary(integer_to_list(1080))}
	end,
	parse_message(Game, Name, Pos, Host, Pw, IPid).
